/**
 * eccodes demo
 * @author shenyc
 */

#include "_main.h"

// function define
// void tools();


int main(int argc, char* argv[])
{
    int err = 0;

    char* filename = "d:/sfxData/ECMWF/eccodes_test_data/reduced_gaussian_model_level.grib1";

    FILE* f = fopen(filename, "rb");

    codes_context* c = codes_context_get_default();
    codes_handle* h  = codes_handle_new_from_file(c, f, PRODUCT_GRIB, &err);
    codes_box* box   = codes_box_new(h, &err);
    char* emsg       = codes_get_error_message(err);

    printf("err message = %s\n", emsg);

    // printf("");

    codes_handle_delete(h);
    fclose(f);

    // sfx_box();
    // sfx_get_product_kind(argc, argv);
    // sfx_grib_clone();
    // sfx_grib_copy_message();
    // sfx_grib_ensemble_index();
    // sfx_grib_get_data();
    // sfx_grib_get_keys();
    // sfx_grib_index();
    // sfx_grib_iterator();
    // sfx_grib_iterator_bitmap();
    // sfx_grib_keys_iterator(argc, argv);
    // sfx_grib_list();
    // sfx_grib_multi();
    // sfx_grib_multi_write();

    // sfx_grib_nearest_multiple();
    // sfx_grib_nearest();

    // sfx_set_missing();
    // sfx_grib_precision();
    // sfx_grib_print_data();
    // sfx_grib_read_from_file();
    // sfx_grib_sections_copy();
    // sfx_grib_set_bitmap();
    // sfx_grib_set_data();
    // sfx_grib_set_keys();
    // sfx_grib_set_pv();
    // sfx_values_check();
    // sfx_large_grib1();
    // sfx_multi2();
    // sfx_mars_param();

    return 0;
}

void tools()
{
}

// ------------------------------------------------------------------

void t_generic_functions()
{
    printf("codes_definition_path = %s \n", codes_definition_path(NULL));
    printf("codes_samples_path = %s \n", codes_samples_path(NULL));
    printf("codes_get_api_version = %d \n", codes_get_api_version());  // 21800
    printf("codes_get_git_sha1 = %s \n", codes_get_git_sha1());
    printf("codes_get_package_name = %s \n", codes_get_package_name());  // ecCodes


    FILE* fin       = fopen(T_FILE_NAME, "rb");
    int err         = 0;
    codes_handle* h = grib_handle_new_from_file(0, fin, &err);

    return;
}

void t_codes_index()
{
    printf("hello, it's t_codes_index.\n\n");

    // int err;
    // char* keys         = "";
    // codes_index* ci = codes_index_new_from_file(0, FILE, keys, &err);
    // codes_index_new_from_file
}


#define MAX_KEY_LEN 255
#define MAX_VAL_LEN 1024
void t_keys_iterator()
{
    /* filter out keys from the key_iterator with
	 * key_iterator_filter_flags  -> GRIB_NO_READ_ONLY_KEYS, GRIB_NO_OPTIONAL_KEYS
	 * set it to 0 if you want all keys
	 */
    char* fn = T_FILE_NAME;
    printf("the input file is %s.\n", fn);

    // char* namespace = 0;
    char* namespace = "mars";

    // unsigned long key_iterator_filter_flags = CODES_KEYS_ITERATOR_SKIP_READ_ONLY || CODES_KEYS_ITERATOR_SKIP_OPTIONAL;
    unsigned long key_iterator_filter_flags = CODES_KEYS_ITERATOR_ALL_KEYS;

    FILE* fin = fopen(fn, "rb");
    if (!fin) {
        perror(fn);
    }

    int err                    = 0;
    int grib_count             = 0;
    codes_handle* h            = NULL;
    codes_keys_iterator* kiter = NULL;

    char value[MAX_VAL_LEN];
    size_t vlen = MAX_VAL_LEN;

    while ((h = codes_handle_new_from_file(0, fin, PRODUCT_GRIB, &err)) != NULL) {
        //
        grib_count++;
        printf("-- GRIB N. %d --\n", grib_count);
        if (!h) {
            printf("ERROR: Unable to create grib handle\n");
        }

        kiter = codes_keys_iterator_new(h, key_iterator_filter_flags, namespace);
        if (!kiter) {
            printf("ERROR: Unable to create keys iterator\n");
        }

        while (grib_keys_iterator_next(kiter)) {
            const char* name = grib_keys_iterator_get_name(kiter);
            vlen             = MAX_VAL_LEN;
            GRIB_CHECK(grib_keys_iterator_get_string(kiter, value, &vlen), name);
            printf("%s = %s\n", name, value);
        }

        grib_keys_iterator_delete(kiter);
    }

    fclose(fin);
    fin = NULL;

    int xtmp = 0;
    return;

    /*

    -- GRIB N. 160 --
    domain = g
    levtype = pl
    levelist = 850
    date = 20170102
    time = 1200
    step = 0
    param = 130.128
    class = ea
    type = an
    stream = enda
    expver = 0001
    number = 9

    */
}
